/**
 * 初始化拜访记录详情对话框
 */
var VisitInfoDlg = {
    visitInfoData: {},
    uploader: null,
    validateFields: {
        beginDate: {
            validators: {
                notEmpty: {
                    message: '开始时间不能为空'
                }
            }
        },
        endDate: {
            validators: {
                notEmpty: {
                    message: '结束时间不能为空'
                },
                date:{
                    format: 'YYYY-MM-DD'
                },
                callback: {
                    message: '结束时间不能小于开始时间',
                    callback: function() {
                        var beginDate=$("#beginTime").val();
                        var endDate=$("#endTime").val();
                        var d1 = new Date(beginDate.replace(/\-/g, "\/"));
                        var d2 = new Date(endDate.replace(/\-/g, "\/"));

                        if(beginDate!=""&&endDate!=""&&d1 > d2)
                        {
                            return false;
                        }
                        return true;
                    }
                }
            }
        },
        customerCompanyName: {
            validators: {
                notEmpty: {
                    message: '企业客户必选'
                }
            }
        },
        target:{
            validators: {
                stringLength: {
                    max:200,
                    message: '必须在200个字符以内'
                }
            }
        },
        summary:{
            validators: {
                stringLength: {
                    max:200,
                    message: '必须在200个字符以内'
                }
            }
        },
        targetPerson:{
            validators: {
                stringLength: {
                    max:20,
                    message: '必须在20个字符以内'
                }
            }
        },
        nextStep:{
            validators: {
                stringLength: {
                    max:200,
                    message: '必须在200个字符以内'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
VisitInfoDlg.clearData = function () {
    this.visitInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
VisitInfoDlg.set = function (key, val) {
    this.visitInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
VisitInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
VisitInfoDlg.close = function () {
    parent.layer.close(window.parent.Visit.layerIndex);
}

/**
 * 收集数据
 */
VisitInfoDlg.collectData = function () {
    var urls = $('input[name="url"]').map(function () {
        return this.value
    }).get();
    this
        .set('id')
        .set('customerCompanyId')
        .set('visitDate')
        .set('beginTime')
        .set('endTime')
        .set('place')
        .set('form')
        .set('visitorId')
        .set('entourage')
        .set('target')
        .set('targetPerson')
        .set('summary')
        .set('nextStep')
        .set('challengesAndOpportunities')
        .set('feedback')
        .set("urls", urls);
}


/**
 * 验证数据是否为空
 * @returns {*|jQuery}
 */
VisitInfoDlg.validate = function () {
    $('#visitContent').data("bootstrapValidator").resetForm();
    $('#visitContent').bootstrapValidator('validate');
    return $("#visitContent").data('bootstrapValidator').isValid();
}
/**
 * 提交添加
 */
VisitInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();
    if(!this.validate()){
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/visit/add", function(data){
        Feng.success("添加成功!");
        window.parent.Visit.table.refresh();
        VisitInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.visitInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
VisitInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/visit/update", function (data) {
        Feng.success("修改成功!");
        window.parent.Visit.table.refresh();
        VisitInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.visitInfoData);
    ajax.start();
}

VisitInfoDlg.showCustomerCompanySelectTree = function () {
    Feng.showInputTree("customerCompanyName", "customerCompanyTreeDiv", 15, 34);
};

VisitInfoDlg.onClickCustomerCompany = function (e, treeId, treeNode) {
    $("#customerCompanyName").attr("value", treeNode.name);
    $("#customerCompanyId").attr("value", treeNode.id);
    $("#customerCompanyTreeDiv").fadeOut("fast");
}

VisitInfoDlg.upload = function () {
    this.uploader.upload();
}

VisitInfoDlg.reShowPic = function(){
    
}

$(function () {

    laydate.render({
        elem: '#visitDate'
    });

    //执行一个laydate实例
    laydate.render({
        elem: '#beginTime',
        type:'time'
    });

    laydate.render({
        elem: '#endTime',
        type:'time'
    });



    Feng.initValidator("visitContent", VisitInfoDlg.validateFields);

    var customerCompanyTree = new $ZTree("customerCompanyTree", "/customerCompany/tree/all");
    customerCompanyTree.bindOnClick(VisitInfoDlg.onClickCustomerCompany);
    customerCompanyTree.init();

    // 初始化图片上传
    var multiPicUpload = new $MultiPicUpload({
        uploadBtnId:"uploadBtn"
    });
    multiPicUpload.init();
    multiPicUpload.picBack($("#pic").val());


});
